<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>爬虫学习05——requests模块 | XiaoZiYan</title><meta name="keywords" content="爬虫,python,requests"><meta name="author" content="XiaoZiYan"><meta name="copyright" content="XiaoZiYan"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="爬虫学习python爬虫requests">
<meta property="og:type" content="article">
<meta property="og:title" content="爬虫学习05——requests模块">
<meta property="og:url" content="https://xiaoziyanxiaoludan.gitee.io/xiaoziyan/2020/10/03/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A005%E2%80%94%E2%80%94requests%E6%A8%A1%E5%9D%97%E5%85%A5%E9%97%A8/index.html">
<meta property="og:site_name" content="XiaoZiYan">
<meta property="og:description" content="爬虫学习python爬虫requests">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://xiaoziyan.top/images/requests.jpeg">
<meta property="article:published_time" content="2020-10-03T12:12:02.000Z">
<meta property="article:modified_time" content="2022-11-03T10:36:00.216Z">
<meta property="article:author" content="XiaoZiYan">
<meta property="article:tag" content="爬虫">
<meta property="article:tag" content="python">
<meta property="article:tag" content="requests">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://xiaoziyan.top/images/requests.jpeg"><link rel="shortcut icon" href="/xiaoziyan/img/favicon.png"><link rel="canonical" href="https://xiaoziyanxiaoludan.gitee.io/xiaoziyan/2020/10/03/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A005%E2%80%94%E2%80%94requests%E6%A8%A1%E5%9D%97%E5%85%A5%E9%97%A8/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta/><link rel="stylesheet" href="/xiaoziyan/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/xiaoziyan/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: XiaoZiYan","link":"链接: ","source":"来源: XiaoZiYan","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#121212","position":"bottom-left"},
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: true,
  islazyload: false,
  isanchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '爬虫学习05——requests模块',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-11-03 18:36:00'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="./css/custom.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome/css/font-awesome.min.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.css"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/xiaoziyan/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/xiaoziyan/archives/"><div class="headline">文章</div><div class="length-num">33</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/xiaoziyan/tags/"><div class="headline">标签</div><div class="length-num">41</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/xiaoziyan/categories/"><div class="headline">分类</div><div class="length-num">39</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/xiaoziyan/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 多媒体</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/xiaoziyan/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/xiaoziyan/movies/"><i class="fa-fw fas fa-video"></i><span> 视频</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('http://xiaoziyan.top/images/requests.jpeg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/xiaoziyan/">XiaoZiYan</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/xiaoziyan/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 多媒体</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/xiaoziyan/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/xiaoziyan/movies/"><i class="fa-fw fas fa-video"></i><span> 视频</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">爬虫学习05——requests模块</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-10-03T12:12:02.000Z" title="发表于 2020-10-03 20:12:02">2020-10-03</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-11-03T10:36:00.216Z" title="更新于 2022-11-03 18:36:00">2022-11-03</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/xiaoziyan/categories/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A0/">爬虫学习</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/xiaoziyan/categories/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A0/python/">python</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/xiaoziyan/categories/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A0/python/%E7%88%AC%E8%99%AB/">爬虫</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/xiaoziyan/categories/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A0/python/%E7%88%AC%E8%99%AB/requests/">requests</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="爬虫学习05——requests模块"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>在前⾯⼩节中, 我们使⽤<code>urllib</code>来抓取⻚⾯源代码. 这个是python内置的⼀个模块. 但是, 它并不是我们常⽤的爬⾍⼯具. 常⽤的抓取⻚⾯的<br>模块通常使⽤⼀个第三⽅模块<code>requests</code>. 这个模块的优势就是⽐<code>urllib</code>还要简单, 并且处理各种请求都⽐较⽅便.既然是第三⽅模块, </p>
<p>那就需要我们对该模块进⾏安装,</p>
<h3 id="安装方法"><a href="#安装方法" class="headerlink" title="安装方法:"></a>安装方法:</h3><figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip install requests</span><br></pre></td></tr></table></figure>

<h3 id="如果安装速度慢的话可以改⽤国内的源进⾏下载安装"><a href="#如果安装速度慢的话可以改⽤国内的源进⾏下载安装" class="headerlink" title="如果安装速度慢的话可以改⽤国内的源进⾏下载安装."></a>如果安装速度慢的话可以改⽤国内的源进⾏下载安装.</h3><figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip install -i https://pypi.tuna.tsinghua.edu.cn/simple requests</span><br></pre></td></tr></table></figure>

<p><img src="http://xiaoziyan.top/images/01.jpg" alt="requests安装"></p>
<p>OK. 接下来我们来看看requests能带给我们什么? 先拿<code>sogou</code>开⼑试试.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 案例1. 抓取搜狗搜索内容</span></span><br><span class="line">kw = <span class="built_in">input</span>(<span class="string">&quot;请输⼊你要搜索的内容:&quot;</span>)</span><br><span class="line">response =requests.get(<span class="string">f&quot;https://www.sogou.com/web?query=<span class="subst">&#123;kw&#125;</span>&quot;</span>) <span class="comment"># 发送get请求</span></span><br><span class="line"><span class="comment"># print(response.text) # 直接拿结果(⽂本)</span></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&quot;sogou.html&quot;</span>, mode=<span class="string">&quot;w&quot;</span>, encoding=<span class="string">&quot;utf-8&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">	f.write(response.text)</span><br></pre></td></tr></table></figure>



<p>接下来, 我们看⼀个稍微复杂那么⼀丢丢的, 百度翻译~注意百度翻译这个url不好弄出来. 记住, 在输⼊的时候, 关掉各种输⼊法,要⽤英⽂输⼊法, 然后不要回⻋. 就能看到这个sug了。</p>
<p><img src="http://xiaoziyan.top/images/image-20210930131935533.png" alt="百度翻译"></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 案例2.抓取百度翻译数据</span></span><br><span class="line"><span class="comment"># 准备参数</span></span><br><span class="line">kw = <span class="built_in">input</span>(<span class="string">&quot;请输⼊你要翻译的英语单词:&quot;</span>)</span><br><span class="line">dic = &#123;</span><br><span class="line">	<span class="string">&quot;kw&quot;</span>: kw <span class="comment"># 这⾥要和抓包⼯具⾥的参数⼀致.</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment"># 请注意百度翻译的sug这个url. 它是通过post⽅式进⾏提交的. 所以我们也要模拟post请求</span></span><br><span class="line">resp = requests.post(<span class="string">&quot;https://fanyi.baidu.com/sug&quot;</span>,data=dic)</span><br><span class="line"><span class="comment"># 返回值是json 那就可以直接解析成json</span></span><br><span class="line">resp_json = resp.json()</span><br><span class="line"><span class="comment"># &#123;&#x27;errno&#x27;: 0, &#x27;data&#x27;: [&#123;&#x27;k&#x27;: &#x27;Apple&#x27;, &#x27;v&#x27;: &#x27;n.苹果公司，原称苹果电脑公司&#x27;....</span></span><br><span class="line"><span class="built_in">print</span>(resp_json[<span class="string">&#x27;data&#x27;</span>][<span class="number">0</span>][<span class="string">&#x27;v&#x27;</span>]) <span class="comment"># 拿到返回字典中的内容</span></span><br></pre></td></tr></table></figure>

<p><img src="http://xiaoziyan.top/images/03.jpg" alt="翻译演示"></p>
<h4 id="是不是很顺⼿呢-还有⼀些⽹站在进⾏请求的时候会校验你的客户端设备型号-⽐如-我们抓取⾖瓣电影"><a href="#是不是很顺⼿呢-还有⼀些⽹站在进⾏请求的时候会校验你的客户端设备型号-⽐如-我们抓取⾖瓣电影" class="headerlink" title="是不是很顺⼿呢? 还有⼀些⽹站在进⾏请求的时候会校验你的客户端设备型号. ⽐如, 我们抓取⾖瓣电影"></a>是不是很顺⼿呢? 还有⼀些⽹站在进⾏请求的时候会校验你的客户端设备型号. ⽐如, 我们抓取⾖瓣电影</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 案例3: 抓取⾖瓣电影</span></span><br><span class="line">url = <span class="string">&#x27;https://movie.douban.com/j/chart/top_list&#x27;</span></span><br><span class="line">param = &#123;</span><br><span class="line">    <span class="string">&#x27;type&#x27;</span>: <span class="string">&#x27;24&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;interval_id&#x27;</span>: <span class="string">&#x27;100:90&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;action&#x27;</span>:<span class="string">&#x27;&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;start&#x27;</span>: <span class="string">&#x27;0&#x27;</span>,<span class="comment">#从库中的第⼏部电影去取</span></span><br><span class="line">    <span class="string">&#x27;limit&#x27;</span>: <span class="string">&#x27;20&#x27;</span>,<span class="comment">#⼀次取出的个数</span></span><br><span class="line">&#125;</span><br><span class="line">headers = &#123;</span><br><span class="line">    <span class="string">&#x27;User-Agent&#x27;</span>: <span class="string">&#x27;Mozilla/5.0 (Macintosh; IntelMac OS X 10_12_0) AppleWebKit/537.36 (KHTML, likeGecko) Chrome/72.0.3626.121 		Safari/537.36&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line">response =</span><br><span class="line">	requests.get(url=url,params=param,headers=headers</span><br><span class="line">)</span><br><span class="line">list_data = response.json()</span><br><span class="line">fp = <span class="built_in">open</span>(<span class="string">&#x27;./douban.json&#x27;</span>,<span class="string">&#x27;w&#x27;</span>,encoding=<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line">json.dump(list_data,fp=fp,ensure_ascii=<span class="literal">False</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&#x27;over!!!&#x27;</span>)</span><br></pre></td></tr></table></figure>

<h4 id="OK-本章和本⼩节的内容就这么多了-简单回顾⼀下本章内容"><a href="#OK-本章和本⼩节的内容就这么多了-简单回顾⼀下本章内容" class="headerlink" title="OK~ 本章和本⼩节的内容就这么多了. 简单回顾⼀下本章内容"></a>OK~ 本章和本⼩节的内容就这么多了. 简单回顾⼀下本章内容</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">* 爬⾍就是写程序去模拟浏览器⽤来抓取互联⽹上的内容</span><br><span class="line">* python中⾃带了⼀个urllib提供给我们进⾏简易爬⾍的编写</span><br><span class="line">* requests模块的简单使⽤, 包括get, post两种⽅式的请求. 以及User-Agent的介绍.</span><br></pre></td></tr></table></figure>

</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">XiaoZiYan</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a target="_blank" rel="noopener" href="https://baidu.com">https://baidu.com</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://xiaoziyanxiaoludan.gitee.io/xiaoziyan" target="_blank">XiaoZiYan</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/xiaoziyan/tags/%E7%88%AC%E8%99%AB/">爬虫</a><a class="post-meta__tags" href="/xiaoziyan/tags/python/">python</a><a class="post-meta__tags" href="/xiaoziyan/tags/requests/">requests</a></div><div class="post_share"><div class="social-share" data-image="http://xiaoziyan.top/images/requests.jpeg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/xiaoziyan/2020/10/04/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A006%E2%80%94%E2%80%94%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/"><img class="prev-cover" src="http://xiaoziyan.top/images/8f6cb19a9c1e6a44bcb597bd5d83cc23.jpeg" onerror="onerror=null;src='/xiaoziyan/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">爬虫学习06——正则表达式</div></div></a></div><div class="next-post pull-right"><a href="/xiaoziyan/2020/10/02/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A004%E2%80%94%E2%80%94HTTP%E5%8D%8F%E8%AE%AE/"><img class="next-cover" src="http://xiaoziyan.top/images/http.jpeg" onerror="onerror=null;src='/xiaoziyan/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">爬虫学习04——HTTP协议</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/xiaoziyan/2020/09/30/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A002%E2%80%94%E2%80%94%E7%99%BE%E5%BA%A6%E9%A1%B5%E9%9D%A2/" title="爬虫学习02——爬整个百度页面"><img class="cover" src="http://xiaoziyan.top/images/baidu.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-09-30</div><div class="title">爬虫学习02——爬整个百度页面</div></div></a></div><div><a href="/xiaoziyan/2020/10/01/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A003%E2%80%94%E2%80%94web%E8%AF%B7%E6%B1%82%E5%85%A8%E8%BF%87%E7%A8%8B%E5%89%96%E6%9E%90/" title="爬虫学习03——web请求全过程剖析"><img class="cover" src="http://xiaoziyan.top/images/web.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-10-01</div><div class="title">爬虫学习03——web请求全过程剖析</div></div></a></div><div><a href="/xiaoziyan/2020/10/02/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A004%E2%80%94%E2%80%94HTTP%E5%8D%8F%E8%AE%AE/" title="爬虫学习04——HTTP协议"><img class="cover" src="http://xiaoziyan.top/images/http.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-10-02</div><div class="title">爬虫学习04——HTTP协议</div></div></a></div><div><a href="/xiaoziyan/2020/10/04/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A006%E2%80%94%E2%80%94%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/" title="爬虫学习06——正则表达式"><img class="cover" src="http://xiaoziyan.top/images/8f6cb19a9c1e6a44bcb597bd5d83cc23.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-10-04</div><div class="title">爬虫学习06——正则表达式</div></div></a></div><div><a href="/xiaoziyan/2020/10/06/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A008%E2%80%94%E2%80%94%E6%89%8B%E5%88%83%E8%B1%86%E7%93%A3TOP250%E7%94%B5%E5%BD%B1%E4%BF%A1%E6%81%AF/" title="爬虫学习08——豆瓣TOP250"><img class="cover" src="http://xiaoziyan.top/images/ceeee12fc03b31eda4dacc44aa21ee96.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-10-06</div><div class="title">爬虫学习08——豆瓣TOP250</div></div></a></div><div><a href="/xiaoziyan/2020/10/07/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A009%E2%80%94%E2%80%94bs4%E8%A7%A3%E6%9E%90-HTML%E8%AF%AD%E6%B3%95/" title="爬虫学习09——bs4解析-HTML语法"><img class="cover" src="http://xiaoziyan.top/images/d9e428449c4bad8f74848c43d771f2ef.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-10-07</div><div class="title">爬虫学习09——bs4解析-HTML语法</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%89%E8%A3%85%E6%96%B9%E6%B3%95"><span class="toc-number">1.</span> <span class="toc-text">安装方法:</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A6%82%E6%9E%9C%E5%AE%89%E8%A3%85%E9%80%9F%E5%BA%A6%E6%85%A2%E7%9A%84%E8%AF%9D%E5%8F%AF%E4%BB%A5%E6%94%B9%E2%BD%A4%E5%9B%BD%E5%86%85%E7%9A%84%E6%BA%90%E8%BF%9B%E2%BE%8F%E4%B8%8B%E8%BD%BD%E5%AE%89%E8%A3%85"><span class="toc-number">2.</span> <span class="toc-text">如果安装速度慢的话可以改⽤国内的源进⾏下载安装.</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%98%AF%E4%B8%8D%E6%98%AF%E5%BE%88%E9%A1%BA%E2%BC%BF%E5%91%A2-%E8%BF%98%E6%9C%89%E2%BC%80%E4%BA%9B%E2%BD%B9%E7%AB%99%E5%9C%A8%E8%BF%9B%E2%BE%8F%E8%AF%B7%E6%B1%82%E7%9A%84%E6%97%B6%E5%80%99%E4%BC%9A%E6%A0%A1%E9%AA%8C%E4%BD%A0%E7%9A%84%E5%AE%A2%E6%88%B7%E7%AB%AF%E8%AE%BE%E5%A4%87%E5%9E%8B%E5%8F%B7-%E2%BD%90%E5%A6%82-%E6%88%91%E4%BB%AC%E6%8A%93%E5%8F%96%E2%BE%96%E7%93%A3%E7%94%B5%E5%BD%B1"><span class="toc-number">2.1.</span> <span class="toc-text">是不是很顺⼿呢? 还有⼀些⽹站在进⾏请求的时候会校验你的客户端设备型号. ⽐如, 我们抓取⾖瓣电影</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#OK-%E6%9C%AC%E7%AB%A0%E5%92%8C%E6%9C%AC%E2%BC%A9%E8%8A%82%E7%9A%84%E5%86%85%E5%AE%B9%E5%B0%B1%E8%BF%99%E4%B9%88%E5%A4%9A%E4%BA%86-%E7%AE%80%E5%8D%95%E5%9B%9E%E9%A1%BE%E2%BC%80%E4%B8%8B%E6%9C%AC%E7%AB%A0%E5%86%85%E5%AE%B9"><span class="toc-number">2.2.</span> <span class="toc-text">OK~ 本章和本⼩节的内容就这么多了. 简单回顾⼀下本章内容</span></a></li></ol></li></ol></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By XiaoZiYan</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><p><a style="margin-inline:5px"target="_blank" href="https://hexo.io/"> <img src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&logo=hexo" title="博客框架为 Hexo" alt="HEXO"></a><a style="margin-inline:5px"target="_blank"href="https://butterfly.js.org/"> <img src="https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&logo=bitdefender" title="主题采用 Butterfly" alt="Butterfly"></a><a style="margin-inline:5px"target="_blank" href="https://www.jsdelivr.com/"> <img src="https://img.shields.io/badge/CDN-jsDelivr-orange?style=flat&logo=jsDelivr" title="本站使用 Jsdelivr 为静态资源提供CDN加速" alt="Jsdelivr"></a><a style="margin-inline:5px"target="_blank" href="https://github.com/"> <img src="https://img.shields.io/badge/Source-gitee-d021d6?style=flat&logo=Gitee" title="本站项目由 Gitee 托管" alt="Gitee"></a><a style="margin-inline:5px"target="_blank"href="http://creativecommons.org/licenses/by-nc-sa/4.0/"> <img src="https://img.shields.io/badge/Copyright-BY--NC--SA%204.0-d42328?style=flat&logo=Claris" alt="img" title="本站采用知识共享署名-非商业性使用-相同方式共享4.0国际许可协议进行许可"></a></p></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/xiaoziyan/js/utils.js"></script><script src="/xiaoziyan/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.autoSpacingPage()
  else {
    getScript('https://cdn.jsdelivr.net/npm/pangu/dist/browser/pangu.min.js')
      .then(() => {
        pangu.autoSpacingPage()
      })
  }
}

function panguInit () {
  if (false){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"></div><script src="./js/sakura.js"></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="true"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><script data-pjax>function electric_clock_injector_config(){
                var parent_div_git = document.getElementsByClassName('sticky_layout')[0];
                var item_html = '<div class="card-widget card-clock"><div class="card-glass"><div class="card-background"><div class="card-content"><div id="hexo_electric_clock"><img id="card-clock-loading" src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-clock/clock/images/weather/loading.gif" style="height: 120px; width: 100%;" data-ll-status="loading" class="entered loading"></div></div></div></div></div>';
                console.log('已挂载electric_clock')
                // parent_div_git.innerHTML=item_html+parent_div_git.innerHTML // 无报错，但不影响使用(支持pjax跳转)
                parent_div_git.insertAdjacentHTML("afterbegin",item_html) // 有报错，但不影响使用(支持pjax跳转)
            }if( document.getElementsByClassName('sticky_layout')[0] && (location.pathname ==='all'|| 'all' ==='all')){

            electric_clock_injector_config()
        } </script><script src="https://pv.sohu.com/cityjson?ie=utf-8"></script><script data-pjax  src="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.js"></script><!-- hexo injector body_end end --></body></html>